var specSelect = {
    template: '#spec-select',
    props: ['goods', 'show'],
    data: function () {
        return {
            stock: 0,
            goods_id: 0,
            image: '',
            specs: [],
            entities: {},
            entity: {},
            number: 1,
            messages: [],
        }
    },
    watch: {
        show: function (show) {
            if (!show) return;

            if (this.goods_id != this.goods.id) {
                this.getEntities();
                this.number = 1;
                this.stock = this.goods.stock;
                this.image = this.goods.image;
                this.specs = [];
                this.messages = this.goods.messages;
            }
            setTimeout(function () {
                $("body").css("overflow", "hidden");
                mui('.mui-scroll-wrapper2').scroll({
                    deceleration: 0.0005, //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
                    scrollY: true, //是否竖向滚动
                    scrollX: false, //是否横向滚动
                    startX: 0, //初始化时滚动至x
                    startY: 0, //初始化时滚动至y
                    bounce: true, //是否启用回弹
                    bounceTime: 500, //回弹动画时间
                    scrollTime: 500,
                    directionLockThreshold: 5,
                    indicators: false
                });
            });
        },
        specs: function (specs) {
            if (!this.checked) return '';
            if (this.goods.use_spec) {
                var spec = [];
                for (var x in specs) {
                    spec.push(specs[x].join(':'));
                }
                this.entity = this.entities[spec.join('|')];
                this.stock = this.entity.stock;
            }

        },
        stock: function (stock) {
            this.number = Math.max(1, this.number);
            this.number = Math.min(stock, this.number);
        },
        number: function (number) {
            if (number < 1) this.number = 1;
            this.number = Math.min(this.stock, number);
        }
    },
    computed: {
        checked: function () {
            if (this.goods.specs.length !== this.specs.length) return false;
            for (var x in this.specs) {
                if (!this.specs[x]) return false;
            }
            return true;
        },
        sell_price:function(){
            return this.entity.sell_price || this.goods.sell_price;
        }
    },
    methods: {
        getEntities: function () {
            var $this = this;
            this.goods_id = this.goods.id;
            $this.entities = {};
            $.get(entityUrl, {goods_id: this.goods.id}, function (result) {
                if (!result.status) return tips(result.message);

                if (!$this.goods.use_spec) {
                    $this.checked = true;
                    $this.entity = result.data[0];
                } else {
                    for (var x in result.data) {
                        var entity = result.data[x];
                        $this.entities[entity.spec_id] = entity;
                    }
                }
            });
        },
        close: function () {
            $("body").css("overflow", "auto");
            this.$emit('update:show', false);
        },
        selectSpec: function (sIndex, vIndex) {
            var spec = this.goods.specs[sIndex];
            var val = spec.values[vIndex];

            vm.select_entity = val.value;
            if (val.image) this.image = val.image;
            this.specs.splice(sIndex, 1, [spec.id, val.id]);
        },
        minus: function () {
            if (this.number < 1) return false;
            this.number--;
        },
        plus: function () {
            this.number++;
        },
        confirm: function () {
            if (!this.checked) return tips('请选择规格属性');
            if (this.stock < 1) return tips('商品库存不足');
            //购物车数量校验
            var num = ShopCart.get(this.entity.id);
            if (this.stock < num || this.stock == num) return tips('库存不足');
            if (this.number < 1) return tips('请选择购买数量');
            //检查商品留言
            if (!this.checkMessages()) return;
            this.close();
            this.$emit('confirm', {
                goods_id: this.goods_id,
                entity_id: this.entity.id,
                number: this.number,
                messages: this.messages
            });
        },
        checkMessages: function () {
            for (var x in this.goods.messages) {
                var msg = this.goods.messages[x];
                if (msg.required && !this.messages[x].value) return tips(msg.name + '不能为空！');
            }
            return true;
        },
        imageChange: function (i) {
            $this = $(event.target);
            var messages = this.messages;
            var file = event.target.files[0];
            if (!checkImageMime(file)) {
                tips('文件类型不允许');
                $this.val('');
                return false;
            }
            if (!checkImageSize(file)) {
                tips('文件超过最大限制' + format_file_size(allowSize));
                $this.val('');
                return false;
            }
            upload_image(file, function (data) {
                messages.splice(i, 1, $.extend(null, messages[i], {
                    thumb_url: data.url_thumb,
                    value: data.thumb
                }));
            });
        }
    }
};

Vue.component('spec-select', specSelect);